package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgBusinessOpportunityDaoMapper;
import com.gg.user.gguser.dao.GgInformationDaoMapper;
import com.gg.user.gguser.dao.GgRemarksDaoMapper;
import com.gg.user.gguser.dao.GgSigningDaoMapper;
import com.gg.user.gguser.service.GgRemarksService;
import com.gg.vo.return_format;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class GgRemarksServiceImpl implements GgRemarksService {

    @Autowired
    private GgRemarksDaoMapper ggRemarksDaoMapper;

    @Autowired
    private GgInformationDaoMapper ggInformationDaoMapper;

    @Autowired
    private GgBusinessOpportunityDaoMapper ggBusinessOpportunityDaoMapper;

    @Autowired
    private GgSigningDaoMapper ggSigningDaoMapper;

    /**
     * 增加备注
     * @param count
     * @param token
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addGgRemarks(HttpServletResponse response, HttpServletRequest request,String count, String token,Integer type) {
        if(count != null && token != null){
            try {
                // 从cookice信息中过去用户信息 在这里只用要id就好
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggId= JSONObject.parseObject(cookiceUser).getString("ggId");
                ggRemarksDaoMapper.add(count, token, ggId);
                // 从信息修改来的
                if(type==1){
                    // 增加冗余修改或者新增列表页面备注
                    ggInformationDaoMapper.updateInformationRemarks(token,count,ggId);
                }else if (type==2){ // 商机修改
                    // 增加冗余修改或者新增列表页面备注
                    ggBusinessOpportunityDaoMapper.updateBusinessRemarks(token,count,ggId);
                }else if (type==3){ // 商机修改
                    ggSigningDaoMapper.updateSigningRemarks(token,count,ggId);
                }
                return return_format.return_format_All(0,"","");
            }catch(Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    public Object getGgRemarkAll(HttpServletResponse response, HttpServletRequest request, String token) {
        if(token != null  ){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
                return return_format.return_format_All(0,ggRemarksDaoMapper.getGgRemarkAll(token, ggId),"");
            }catch(Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }
}
